package com.sunpy.niohttp.model;


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class AnnotationInfo {

    /**
     * 注解名称
     */
    private String annotationName;

    /**
     * 多个注解参数
     */
    private Map<String, String> param = new HashMap<>();

    public AnnotationInfo() {
    }

    public AnnotationInfo(String annotationName) {
        this.annotationName = annotationName;
    }

    public AnnotationInfo(String annotationName, Map<String, String> param) {
        this.annotationName = annotationName;
        this.param = param;
    }

    public String getAnnotationName() {
        return annotationName;
    }

    public void setAnnotationName(String annotationName) {
        this.annotationName = annotationName;
    }

    public Map<String, String> getParam() {
        return param;
    }

    public void setParam(Map<String, String> param) {
        this.param = param;
    }

    public <T extends Annotation> void setAnnotation(T t) throws Exception {
        AnnotationInfo annotationInfo = new AnnotationInfo();
        Class<? extends Annotation> clazz = t.annotationType();
        annotationInfo.setAnnotationName(clazz.getName());

        Map<String, String> requestPathParam = new HashMap<>();
        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            if (method != null) {
                method.setAccessible(true);
                requestPathParam.put(method.getName(), method.invoke(t).toString());
            }
        }

        annotationInfo.setParam(requestPathParam);
    }

    @Override
    public String toString() {
        return "AnnotationInfo{" +
                "annotationName='" + annotationName + '\'' +
                ", param=" + param +
                '}';
    }
}
